<?php
namespace app\index\controller;
use app\common\controller\HomeBase;
use app\common\model\Article;
use app\common\model\User;
use app\common\model\Comment;
use think\Cache;
use think\Db;

class Mapi extends HomeBase
{
    protected $siteConfig;
    protected $params;
    public function _initialize()
    {
        parent::_initialize();
        $this->params = $this->request->param();
        $this->siteConfig = Cache::get('site_config');
    }

    public function get_all_list(){
        $params = $this->params;
        if (isset($params['page'])) {
            $page = $params['page'];
            $limit =  10;
            $count = (new Article())->where("status", 1)->count();
            $data['limit'] = $limit;
            $data['pages'] = ceil($count / $limit);
            $pre = ($page - 1) * $limit;
            $list = (new Article())->alias('a')
                ->join('article_category b', 'a.category_id=b.id', 'left')
                ->join('user c', 'a.user_id=c.id')
                ->where("a.status", 1)
                ->limit($pre,$limit)
                ->order('id', 'desc')->field('a.*,b.name,c.nickname')
                ->select();
            return json(array(
                'code' => 1,
                'msg' => 'success',
                'data' => [
                    'count'=>$count,
                    'pages'=>ceil($count / $limit),
                    'data'=>$list,
                ]
            ));
        }
    }

    public function user_list_page(){

        $_data = $this->params;
        if (isset($_data['page'])) {
            $page = $_data['page'];
            $uuid = $_data['uuid'];
            $limit =  10;
            $userId = User::get(['uuid'=>$uuid])->id;
            $count = (new Article())->where('user_id', $userId)->count();
            $data['limit'] = $limit;
            $data['pages'] = ceil($count / $limit);
            $pre = ($page - 1) * $limit;

            $list = (new Article())->where('user_id', $userId)->limit($pre,$limit)->select();
            $data['list'] = $list;
            $data['count'] = count($list);
            return json($data);
        }

    }

    public function user_pl_page(){

        $_data = $this->params;
        if (isset($_data['page'])) {
            $page = $_data['page'];
            $uuid = $_data['uuid'];
            $limit =  10;
            $userId = User::get(['uuid'=>$uuid])->id;
            $count = (new Comment())->where('user_id', $userId)->count();
            $data['limit'] = $limit;
            $data['pages'] = ceil($count / $limit);
            $pre = ($page - 1) * $limit;
            $list = (new Comment())->alias('a')->join('article b','a.object_id=b.id')
                ->where('a.user_id', $userId)
                ->limit($pre,$limit)
                ->field('a.content,a.created_at,b.title,b.id')->select();
            $data['list'] = $list;
            $data['count'] = count($list);
            return json($data);
        }

    }
    public function search(){

        $params = $this->params;
        $keyword=remove_xss($params['keyword']);
        if (isset($params['page'])) {
            $page = $params['page'];
            $limit =  10;
            $count = (new Article())->where("status", 1)->where('title', 'like', '%' . $keyword . '%')->count();
            $data['limit'] = $limit;
            $data['pages'] = ceil($count / $limit);
            $pre = ($page - 1) * $limit;
            $list = (new Article())->alias('a')
                ->join('article_category b', 'a.category_id=b.id', 'left')
                ->join('user c', 'a.user_id=c.id')
                ->where("a.status", 1)->where('title', 'like', '%' . $keyword . '%')
                ->limit($pre,$limit)
                ->order('id', 'desc')->field('a.*,b.name,c.nickname')
                ->select();


            return json(array(
                'code' => 1,
                'msg' => 'success',
                'data' => [
                    'count'=>$count,
                    'pages'=>ceil($count / $limit),
                    'data'=>$list,
                ]
            ));
        }

    }
}
